﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XinGuanERP.Attributes.system;
using XinGuanERP.Models.Vo.system;
using XinGuanERP.Services.system.IServices;
using XinGuanERP.Utils;

namespace XinGuanERP.Controllers.system
{
    [DynamicApiController]
    public class RoleController
    {
        private readonly IRoleService _roleService;
        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil PostRole(RoleVo viewRole) =>
            ResultUtil.ok(_roleService.UpdateRole(viewRole));

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil GetRoleWithList(string? roleName, string? status)
        {
            return ResultUtil.ok(_roleService.GetRoleWithList(roleName, status));
        }

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil GetRoleWithPage(int page, int pageSize, string? roleName, string? status)
        {
            return ResultUtil.ok(_roleService.GetRoleWithPage(page, pageSize, roleName, status));
        }

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil DeleteRole(string id) =>
            ResultUtil.ok(_roleService.DeleteRole(id));

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        [HttpPut]
        public ResultUtil SetRoleStatus(RoleVo viewRole) =>
            ResultUtil.ok(_roleService.SetRoleStatus(viewRole.Id, viewRole.Status));

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil GetSysMenuByRoleIdWithOptionTree(string roleId)
            => ResultUtil.ok(_roleService.GetSysMenuByRoleIdWithOptionTree(roleId));

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil PostAssignMenu(SysRoleMenuVo viewSysRoleMenu)
                => ResultUtil.ok(_roleService.AssignMenu(viewSysRoleMenu.MenuIds, viewSysRoleMenu.RoleId));
    }
}
